package com.xdtech.trans.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xdtech.common.utils.EmptyUtil;
import com.xdtech.common.utils.EncryptUtil;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.sys.model.Role;
import com.xdtech.sys.model.UserGroup;
import com.xdtech.sys.service.RoleService;
import com.xdtech.sys.service.UserGroupService;
import com.xdtech.sys.service.UserService;
import com.xdtech.sys.vo.UserItem;
import com.xdtech.trans.dao.PersonDao;
import com.xdtech.trans.model.Person;
import com.xdtech.trans.model.TransWebSite;
import com.xdtech.trans.service.PersonService;
import com.xdtech.trans.service.TransWebSiteService;
import com.xdtech.trans.vo.PersonItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-05-28 00:20:05
 * @since 1.0
 * @see
 */
@Service
public class PersonServiceImpl implements PersonService {
	private Log log = LogFactory.getLog(PersonServiceImpl.class);
	@Autowired
	private PersonDao personDao;
	@Autowired
	private BaseDao baseDao;
	@Autowired
	private UserGroupService userGroupService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private TransWebSiteService transWebSiteService;
	@Autowired
	private UserService userService;
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param entity
	 */
	public void save(Person entity) {
		personDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<Person> entities) {
		personDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param entity
	 */
	public void delete(Person entity) {
		personDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		personDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param id
	 * @return
	 */
	public Person get(Long id) {
		return personDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @return
	 */
	public List<Person> getAll() {
		return personDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> persons = null;
		long rows = 0;
		if (pg!=null) {
			Page<Person> page = personDao.findPage(pg,"from Person order by createTime desc", values);
			persons = BeanUtils.copyListProperties(
					PersonItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<Person> personList = personDao.find("from Person order by id desc", values);
			persons = BeanUtils.copyListProperties(
					PersonItem.class, personList);
			rows = persons.size();
		}
		results.put("total", rows);
		results.put("rows", persons);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdatePerson(PersonItem item) {
		Person person = null;
		if (item.getId()==null) {
			person = new Person();
			person.setPassword(EncryptUtil.encodePassword("123456"));
		}else {
			person = personDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(person, item);
		List<Long> selectGroupIds = new ArrayList<Long>();
		if (StringUtils.isNotEmpty(item.getUserGroup())) {
			String[] groupIds = item.getUserGroup().split(",");
			for (String gid : groupIds) {
				selectGroupIds.add(Long.valueOf(gid));
			}
		}
		if (EmptyUtil.isNotEmpty(selectGroupIds)) {
			List<UserGroup> userGroups = userGroupService.loadUserGroupsByIds(selectGroupIds);
			person.setUserGroups(userGroups);
		}else
		{
			person.setUserGroups(null);
		}
		
//		List<Long> selectRoleIds = new ArrayList<Long>();
//		if (StringUtils.isNotEmpty(item.getRole())) {
//			String[] roleIds = item.getRole().split(",");
//			for (String rid : roleIds) {
//				selectRoleIds.add(Long.valueOf(rid));
//			}
//		}
//		if (EmptyUtil.isNotEmpty(selectRoleIds)) {
//			List<Role> roles = roleService.loadRoleByIds(selectRoleIds);
//			person.setRoles(roles);
//		}else
//		{
//			person.setRoles(null);
//		}
		//写死网点员工的角色
		Role role = roleService.getRoleByCode("wangdian");
		if (role!=null) {
			List<Role> roles = new ArrayList<Role>();
			roles.add(role);
			person.setRoles(roles);
		}else {
			person.setRoles(null);
		}
		if (StringUtils.isNotBlank(item.getWebsiteId())) {
			TransWebSite transWebSite = transWebSiteService.get(Long.valueOf(item.getWebsiteId()));
			person.setWebSite(transWebSite);
		}
		personDao.save(person);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param personId
	 * @return
	 */
	public PersonItem loadPersonItem(Long personId) {
		Person person = personDao.get(personId);
		PersonItem personItem = PersonItem.createItem(person);
		return personItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deletePersonInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deletePersonInfo(List<Long> personIds) {
		for (Long id : personIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-05-28 00:20:05
	 * @modified by
	 * @return
	 */
	@Override
	public List<PersonItem> loadItems() {
		List<Person> persons = getAll();
		return PersonItem.createItems(persons);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-7-4下午11:23:24
	 * @modified by
	 * @param username
	 * @param password
	 * @return
	 */
	@Override
	public PersonItem loginCheck(String username, String password) {
		String entry = EncryptUtil.encodePassword(password);
		UserItem userItem = userService.getUserWithNameAndPwd(username,entry);
		if (userItem!=null) {
			PersonItem personItem = loadPersonItem(userItem.getId());
			return personItem;
		}else {
			return null;
		}
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-7-10下午9:35:29
	 * @modified by
	 * @param id
	 * @return
	 */
	@Override
	public PersonItem getPersonItem(Long id) {
		Person person = get(id);
		return person!=null?PersonItem.createItem(person):null;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-7-21下午11:10:01
	 * @modified by
	 * @param webId
	 * @return
	 */
	@Override
	public List<Person> getPersonsByCurrentWebsite(Long webId) {
		return getAll();
	}

}
